package actions;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import db.dao.Classes;
import db.dao.ClassesDAO;
import db.dao.Dept;
import db.dao.DeptDAO;
import db.dao.Major;
import db.dao.MajorDAO;
import db.dao.Users;
import db.dao.UsersDAO;

/*查找班级信息*/
public class SearchClassesAction extends ActionSupport{
	private String classOption="";	//查找选项
	private String property="";	//值
	
	public SearchClassesAction() {
	}

	public String getClassOption() {
		return classOption;
	}

	public void setClassOption(String classOption) {
		this.classOption = classOption;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public String execute() throws IOException {
		//获取页面信息，用于弹窗提示和清除页面缓存
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		//用于传递参数，判断查询内容
		HttpSession session = request.getSession();
		//根据用户选择的查询条件和输入的值查找班级信息
		ClassesDAO cDao = new ClassesDAO();
		Set<Classes> list = new HashSet<Classes>();
		if (classOption.equals("id")) {
			Classes classes = cDao.findById(property);
			if (classes!=null) {
				list.add(classes);
			}
		}else if (classOption.equals("dept")) {
			DeptDAO dDao = new DeptDAO();
			Dept dept = dDao.findById(property);
			if (dept!=null) {
				list = dept.getClasseses();
			}	
		}else if (classOption.equals("major")) {
			MajorDAO mDao = new MajorDAO();
			Major major = mDao.findById(property);
			if (major!=null) {
				list = major.getClasseses();
			}
		}
		if (!list.isEmpty()) {
			//成功传递信息
			session.setAttribute("classes", list);
			session.setAttribute("search", new String("classes"));
			response.setHeader("Pragma","No-cache");   
			response.setHeader("Cache-Control","no-cache");  
			response.setHeader("Cache-Control", "no-store");  
			response.setDateHeader("Expires", 0);
			return "success";
		}else {
			//失败弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('未查询到相关信息!')</script>");
            UsersDAO uDao = new UsersDAO();
            Users users = uDao.findById((String)session.getAttribute("username"));
            if (users.getUsertype().equals("管理")) {
				printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
			}else if (users.getUsertype().equals("教师")) {
				printWriter.print("<script>window.location.href='/GroupProject/teacher.jsp'</script>");
			}else {
				printWriter.print("<script>window.location.href='/GroupProject/student.jsp'</script>");
			}
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}
	}
}
